package com.yuwen.yuwenRPC.fault;

import com.github.rholder.retry.*;
import com.yuwen.yuwenRPC.model.RpcResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @ClassNameFixedIntervalRetryStrategy
 * @Description TODO
 * @Author钰玟
 * @Date2025/4/6 上午10:38
 * @Version 1.0
 **/


/**
 * 固定间隔的重试机制
 */
@Slf4j
public class FixedIntervalRetryStrategy implements RetryStrategy {
    @Override
    public RpcResponse doRetry(Callable<RpcResponse> callable) throws Exception {

        Retryer<RpcResponse> retryer = RetryerBuilder.<RpcResponse>newBuilder()
                .retryIfExceptionOfType(Exception.class)//重试的异常错误类型
                .withWaitStrategy(WaitStrategies.fixedWait(3l, TimeUnit.SECONDS))//重试策略
                .withStopStrategy(StopStrategies.stopAfterAttempt(3))//重试停止
                .withRetryListener(new RetryListener() {
                    @Override
                    public <V> void onRetry(Attempt<V> attempt) {
                        log.info("重试次数,{}",attempt.getAttemptNumber());
                    }
                })//重试工作
                .build();
        return retryer.call(callable);
    }
}
